﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Events;
using Trippy.Abilities;
using Trippy.CollidableGameComponents.Agents;

namespace Trippy.UI_Overlay
{
    class GameOver : TrippyState
    {
        List<MenuWindow> menuList = new List<MenuWindow>();
        public new TrippyGame Game { get; protected set; }
        MenuWindow menuMain;
        MenuWindow activeMenu;
        bool menusRunning = true;
        Levels.Level currentGame;
        MenuWindow closeGame;
        MenuWindow menuNewGame;
        KeyboardState CurKeyState;
        KeyboardState PrevKeyState;
        private SpriteBatch spriteBatch;

        public GameOver(TrippyGame game, Levels.Level level)
            : base(game)
        {
            Game = game;
            currentGame = level;
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
        }
        protected override void LoadContent()
        {
            SpriteFont menuFont = Game.Content.Load<SpriteFont>("Fonts/UI");
            Texture2D background = Game.Content.Load<Texture2D>("Background/abstract_bkg");
            //Creating menuItems and adding to the menuList
            menuMain = new MenuWindow(menuFont, "Game Over!", background);
            menuNewGame = new MenuWindow(null, null, null);
            closeGame = new MenuWindow(null, null, null);
            activeMenu = menuMain;
            menuList.Add(menuMain);
            menuList.Add(menuNewGame);

            menuMain.AddMenuItem("Continue", menuNewGame);
            menuMain.AddMenuItem("Quit", closeGame);

            menuMain.WakeUp();
        }

        public override void Update(GameTime gameTime)
        {
            CurKeyState = Keyboard.GetState();

            if (menusRunning)
            {
                foreach (MenuWindow currentMenu in menuList)
                    currentMenu.Update(gameTime.ElapsedGameTime.TotalMilliseconds);
                MenuInput(CurKeyState);
            }
            else { }
            base.Update(gameTime);
            PrevKeyState = CurKeyState;
        }

        public override void Draw(GameTime gametime)
        {
            spriteBatch.Begin();
            foreach (MenuWindow currentMenu in menuList)
                currentMenu.Draw(spriteBatch);
            spriteBatch.End();
        }

        private void MenuInput(KeyboardState currentKeybState)
        {
            MenuWindow newActive = activeMenu.ProcessInput(PrevKeyState, currentKeybState);

            if (newActive == menuNewGame)
            {
                menusRunning = false;
                Hero.Lives = 10;
                Hero.Score = 0;
                currentGame.Hero.CurrentAbility = null;
                Hero.PreviousAbility = null;
                menusRunning = false;
                var firstLevel = new Levels.FirstLevel(Game, null); // if no hero is passed as a parameter, one is automatically created
                Game.Components.Clear();
                Game.Components.Add(firstLevel);
                Game.currentState = firstLevel;
            }
            if (newActive == closeGame)
                Game.Exit();
            if (newActive == null)
                Game.Exit();
            else if (newActive != activeMenu)
                newActive.WakeUp();
            activeMenu = newActive;
        }
    }
}
